

class wilberforce.util.DateUtility
{
	
	private static var dayOfWeekArray:Array=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
	private static var monthArray:Array=["January","February","March","April","May","June","July","August","September","October","November","December"];
	private static var daysInMonthArray:Array=[31,28,31,30,31,30,31,31,30,31,30,31];
	
	static function isLeapYear(year:Number):Boolean
	{
		
		if (year<1900) year+=1900;
		
		if (year%400==0) return true;
		else if (year%100==0) return false;
		else if (year%4==0) return true;
		return false;
	}
	
	static function daysInMonth(month:Number,year:Number):Number
	{
		var tDaysInMonth:Number=daysInMonthArray[month];
		var tLeapYear:Boolean=isLeapYear(year);
		if (tLeapYear && month==1) tDaysInMonth=29;
		return tDaysInMonth;
	}
	
	static function getDayName(index:Number):String
	{
		return dayOfWeekArray[index];
	}
	
	static function getMonthName(index:Number):String
	{
		return monthArray[index];
	}
	
	static function getOffsetDate(date:Date,dayOffset:Number):Date
	{
		var tDay:Number=date.getDate();		var tMonth:Number=date.getMonth();		var tYear:Number=date.getYear();
		if (tYear<1000) tYear+=1900;
		
		// Round to be safe
		
		
		dayOffset=Math.round(dayOffset);
		var tTotalIncrements:Number=Math.abs(dayOffset);
		var tIncrement:Number=dayOffset/tTotalIncrements;
		var tDaysInMonth:Number=daysInMonth(tMonth,tYear);
		
		while (tTotalIncrements>0)
		{			
			tDay+=tIncrement;
			
			if (tDay>tDaysInMonth)
			{
				tMonth++;
				tDay=1;
				
				if (tMonth>11)
				{
					tMonth=0;					tYear++;
				}
				tDaysInMonth=daysInMonth(tMonth,tYear);
			}
			if (tDay<=0)
			{
				tMonth--;
				if (tMonth<0)
				{
					tMonth=11;
					tYear--;
				}					
				tDay=tDaysInMonth=daysInMonth(tMonth,tYear);				
			}
			
			tTotalIncrements--;
		}
		trace("YEAR IS "+tYear);
		return new Date(tYear,tMonth,tDay);
	}
	
}